package com.yzy.utils;

import java.sql.*;

public class Utils {
	 Connection con;
	
	static String connectDB ;
	
	static String user;
	
	static String password;

	 PreparedStatement ps;
	
	
	static{
		String JDriver = "com.mysql.cj.jdbc.Driver";
		connectDB  = "jdbc:mysql://localhost/hospital?serverTimezone=Asia/Shanghai&useSSL=true";
		try {
			Class.forName(JDriver);
		} catch (ClassNotFoundException e) {

			System.out.println("加载数据库引擎失败");

		}
		System.out.println("数据库驱动成功");

		user = "root";
		password = "123";
	}
	
	public  PreparedStatement createStatement(String sql) {
		try {
			ps = getCon().prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ps;
	}

	public Connection getCon(){
		

		try {
			con = DriverManager.getConnection(connectDB, user, password);

			System.out.println("连接数据库成功");

		} catch (SQLException e) {

			e.printStackTrace();
		}
		return con;
	}
	public void close(){
		try {
			if(ps !=null) {
				ps.close();
				System.out.println("rs colsed");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if(con !=null) {
				con.close();
				System.out.println("con colsed");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public  void close(ResultSet rs){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
